#pragma once

#include <QObject> 
#include <QTimer>
#include <QTcpSocket>
#include <QMutex>
#include <QObject>
#include <QThread>

#include "commonDefine.h"

namespace CommonComponet
{
    class TcpClient : public QObject
    {
        Q_OBJECT
    public:
        TcpClient(QSharedPointer<CommonComponet::TcpConnectInfo> tcp_connect_info, DEVICE_CONN_TYPE tcpType, QString end_string, int timeout, int reconnect_timer, DataMode mode = TextMode);
        ~TcpClient();

        void sendBinaryData(const QByteArray& data);
        QByteArray sendAndWaitBinary(const QByteArray& data, int write_timeout = 3000, int read_timeout = 3000);

    public slots:
        void Started();
        void updateTimer();

    public slots:
        void slot_reconnect_server();
        void slot_reconnect_server_by_newconfig(const QString& ip, int port);
        void slot_send_message(QString message); 
        void slot_async_timeout_send_message(QString message, int write_timeout, int read_timeout);
        QString slot_send_and_wait_message(QString message, int write_timeout, int read_timeout);

    signals:
        void sig_connection_state(int state); 
        void sig_receiveDataFromServer(QString);
        void sig_receiveBinaryData(const QByteArray&);   // 二进制数据信号

     //internal slots
    private slots:
        void slot_recvMessage();
        void slot_SocketState(QAbstractSocket::SocketState state);

        //control
    private:
        QTcpSocket* m_tcpSocket;
        DEVICE_CONN_TYPE m_TcpType;
        DataMode m_dataMode;  // 数据模式

    private:
        int m_disconnect_time = 0;
    private:
        CommonComponet::DeviceConnState m_tcp_state;
        QString m_end_string;
        QString m_receive_string_package;
        int m_connect_server_time_out;
        int m_reconnect_timer; 

    private:
        QSharedPointer<CommonComponet::TcpConnectInfo> m_tcp_connect_info;
        QTimer* m_timer = NULL;
    };
}

